package com.sneaker.shower.controller;

import com.sneaker.shower.common.BaseView;
import com.sneaker.shower.entity.dto.*;
import com.sneaker.shower.entity.dto.coupon.CouponAddReqDTO;
import com.sneaker.shower.entity.enums.KuaiDiCompanyEnum;
import com.sneaker.shower.entity.enums.SubOrderStatusEnum;
import com.sneaker.shower.service.SubOrderService;
import com.sneaker.shower.util.JacksonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Hechangjian
 * @version 1.0
 * @className SubOrderController
 * @date 2019/07/29 13:35
 * @description 订单API
 * @program bath
 */
@Api("订单API")
@RequestMapping("/sub_order")
@RestController
@Slf4j
public class SubOrderController {

    private final SubOrderService subOrderService;

    @Autowired
    public SubOrderController(SubOrderService subOrderService) {
        this.subOrderService = subOrderService;
    }

    @PostMapping("/cart_submit")
    @ApiOperation("购物车下单到我的订单")
    public BaseView cartSubmit(@RequestBody SubOrderGuidSCommitDTO subOrderGuidSCommitDTO) {
        log.info("购物车下单请求入参：{}", JacksonUtils.writeValueAsString(subOrderGuidSCommitDTO));
        subOrderService.cartSubmit(subOrderGuidSCommitDTO);
        return new BaseView();
    }

    @PostMapping("/commit")
    @ApiOperation(value = "提交订单，这个时候要上传图片，往客服发消息")
    public BaseView commit(@RequestBody SubOrderDTO subOrderDTO) {
        subOrderService.commit(subOrderDTO);
        return new BaseView();
    }

    @PostMapping("/submit")
    @ApiOperation(value = "直接下单，无需进入购物车")
    public BaseView<Boolean> submit(@RequestBody SubOrderDTO subOrderDTO) {
        return new BaseView<>(subOrderService.submit(subOrderDTO, null, Boolean.FALSE, SubOrderStatusEnum.WAIT_FOR_SUBMITTING.getCode(), subOrderDTO.getGoodsItemDTOList().get(0).getType()));
    }

    @PostMapping("/fill_tracking_number")
    @ApiOperation("添加快递单号")
    public BaseView<Boolean> fullTrackingNumber(@RequestBody TrackingNumberDTO trackingNumberDTO) {

        return new BaseView<>(subOrderService.fillTrackingNumber(trackingNumberDTO));
    }

    @GetMapping("/get_all")
    @ApiOperation("获取当前用户所有订单信息")
    public BaseView<List<SubOrderDTO>> getAll(@ApiParam("2、待确认，5、待支付，9、待发货，11、待收货，不传表示查所有") Integer type) {
        return new BaseView<>(subOrderService.getAll(type));
    }

    @GetMapping("/get/{GUID}")
    @ApiOperation("通过guid获取订单详情")
    public BaseView<SubOrderDTO> get(@PathVariable("GUID") String guid) {
        log.info("查询订单详情请求入参：{}", guid);
        return new BaseView<>(subOrderService.getByGuid(guid));
    }

    @GetMapping("/get_count")
    @ApiOperation("查询当前用户订单数量")
    public BaseView<OrderCountDTO> getCount() {

        return new BaseView<>(subOrderService.getCount());
    }

    @GetMapping(value = "/kuaidi")
    @ApiOperation("查询快递信息")
    public BaseView<KuaiDiDTO> kuaidi(@RequestParam String guid) {
        return new BaseView<>(subOrderService.findKuaiDi(guid));
    }

    @GetMapping(value = "/kuaidi/company")
    @ApiOperation("查询快递信息")
    public BaseView<List<KuaiDiCompanyDTO>> company() {
        return new BaseView<>(
                Arrays.stream(KuaiDiCompanyEnum.values()).map((e) ->
                        new KuaiDiCompanyDTO(e.getCode(), e.getName())
                ).collect(Collectors.toList())
        );
    }

    @GetMapping("/finish")
    public BaseView<Boolean> finish(String guid) {
        log.info("guid:{}", guid);
        return new BaseView<>(subOrderService.finish(guid));
    }

    @PostMapping("/add_or_update_coupon")
    public BaseView<Boolean> addOrUpdateCoupon(@RequestBody CouponAddReqDTO couponAddReqDTO) {
        return new BaseView<>(subOrderService.addOrUpdateCoupon(couponAddReqDTO));
    }
}
